package com.blog.admin.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;

import java.util.Calendar;
import java.util.Map;

public class JWTUtil {

    private static final String SECRET = "123456Mszlu!@#$$";

    public static String getToken(Map<String,String> payload) {

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 7);

        JWTCreator.Builder builder = JWT.create();

        payload.forEach((k,v) ->{
            builder.withClaim(k,v);
        });
        builder.withExpiresAt(calendar.getTime());

        return builder.sign(Algorithm.HMAC256(SECRET));
    }

    public static Map<String, Claim> checkToken(String token) {
        try {
            Map<String, Claim> claimMap = JWT.require(Algorithm.HMAC256(SECRET)).build().verify(token).getClaims();
            return claimMap;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
